<?php

class CourseSearchController extends Controller {
    
    private $_indexFiles = 'runtime.search';
    
    public function init() {
        Yii::import('application.vendors.*');
        require_once ('Zend/Search/Lucene.php');
        parent::init();
    }
    
    /*
     * search index creation
     */
    public function actionCreate() {
        
        $this->_indexFiles = 'runtime.search';
        
        // create a Zend_Search_Lucene object
        // The first arguemnt is the location of the database file
        // The second argument indicates that a fresh database should be created
        $index = new Zend_Search_Lucene(Yii::getPathOfAlias('application.' . $this->_indexFiles), true);
        
        $course = Course::model()->findAll();
        
        // search each post by title, links and content
        foreach ($course as $c) {
            $doc = new Zend_Search_Lucene_Document();
            $doc->addField(Zend_Search_Lucene_Field::text('course_id', CHtml::encode($c->course_id), 'utf-8'));
            $doc->addField(Zend_Search_Lucene_Field::keyword('course_nr', CHtml::encode($c->course_nr), 'utf-8'));
//            $doc->addField(Zend_Search_Lucene_Field::text('center', CHtml::encode($c->center), 'utf-8'));
//            $doc->addField(Zend_Search_Lucene_Field::text('category1', CHtml::encode($c->category1), 'utf-8'));
//            $doc->addField(Zend_Search_Lucene_Field::text('category2', CHtml::encode($c->category2), 'utf-8'));
//            $doc->addField(Zend_Search_Lucene_Field::text('type', CHtml::encode($c->type), 'utf-8'));
            $doc->addField(Zend_Search_Lucene_Field::text('title', CHtml::encode($c->title), 'utf-8'));
//            $doc->addField(Zend_Search_Lucene_Field::text('requirement', CHtml::encode($c->requirement), 'utf-8'));
            $doc->addField(Zend_Search_Lucene_Field::text('start', CHtml::encode($c->start), 'utf-8'));
            $doc->addField(Zend_Search_Lucene_Field::text('end', CHtml::encode($c->end), 'utf-8'));
            $doc->addField(Zend_Search_Lucene_Field::text('status', CHtml::encode($c->status), 'utf-8'));
//            $doc->addField(Zend_Search_Lucene_Field::text('link', CHtml::encode($c->url), 'utf-8'));
            Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive());
           $index->addDocument($doc);
        }
        
        $index->commit(); // save the database
        echo 'Lucene index created';
                
    }
    
    /*
     * This action would check from search terms, run the search against the db and 
     * send the results on to a View
     */
    public function actionCourseSearch() {
        // check for the presence of search terms in the url
        if (isset($_POST['q'])) {
//            $term = strtolower($_POST['q']);
            $term = $_POST['q'];
            Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive());
            // create the Zend_Search_Lucene object and provide the location of the db
            $index = new Zend_Search_Lucene(Yii::getPathOfAlias('application.' . $this->_indexFiles));
            
            Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength(0);
            
            // perform the search
            $pattern = new Zend_Search_Lucene_Index_Term('*'.$term.'*');
            $query = new Zend_Search_Lucene_Search_Query_Wildcard($pattern);
            $results = $index->find($query);
          
            // render the view, passing it the results
            $this->renderPartial('courseSearch', compact('results', 'term', 'query'));
        }
    }
    
}

?>
